package org.atm.kxp.view.gui;

import org.atm.kxp.controller.LoginController;
import org.atm.kxp.model.Customer;

import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Gui extends JFrame {

	private JButton jButtonLogin;
	private JLabel jLabelCid;
	private JLabel jLabelCpin;
	private JPanel jPanel;
	private JPanel jPanelBtn;
	private JPanel jPanelCid;
	private JPanel jPanelCpin;
	private JTextField jTextFieldCid;
	private JPasswordField jTextFieldCpin;

	private LoginController loginController = new LoginController();

	private static final int min_len = 6;
	private static final int max_len = 12;

	public Gui() {
		this.setTitle("cstatm212219 蒯兴鹏");
		this.setSize(300, 200);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);

		jPanel = new JPanel(new GridLayout(3, 1));
		jPanelCid = new JPanel();
		jLabelCid = new JLabel("CID");
		jLabelCid.setForeground(Color.GREEN);
		jLabelCid.setFont(new Font("", Font.BOLD, 15));
		jTextFieldCid = new JTextField(15);
		jPanelCid.add(jLabelCid);
		jPanelCid.add(jTextFieldCid);

		jPanelCpin = new JPanel();
		jLabelCpin = new JLabel("CPIN");
		jLabelCpin.setForeground(Color.GREEN);
		jLabelCpin.setFont(new Font("", Font.BOLD, 15));
		jTextFieldCpin = new JPasswordField(15);
		jPanelCpin.add(jLabelCpin);
		jPanelCpin.add(jTextFieldCpin);

		jPanelBtn = new JPanel();
		jButtonLogin = new JButton("LOGIN");
		jPanelBtn.add(jButtonLogin);
		jPanel.add(jPanelCid);
		jPanel.add(jPanelCpin);
		jPanel.add(jPanelBtn);
		this.add(jPanel);
		this.setVisible(true);
		jButtonLogin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String cid = new String(jTextFieldCid.getText());
				if (cid.length() <= 0) {
					JOptionPane.showMessageDialog(null, "CID NULL");
					System.out.println("CID NULL");
				} else {
					String cpin = new String(jTextFieldCpin.getPassword());
					Customer c = new Customer(cid, cpin);
					Customer isLogin = null;
					if (lengthValidate(cid) && lengthValidate(cpin)
							&& isNumeric(cid) && isNumeric(cpin))
						isLogin = loginController.login(c);
					if (isLogin != null) {
						JOptionPane.showMessageDialog(null, "LOGIN SUCCEEDED! kxp", "PROMPT",
								JOptionPane.INFORMATION_MESSAGE);
					} else {
						JOptionPane.showMessageDialog(null, "ID OR PIN ERROR!");
					}
				}
			}
		});
	}

	public static void main(String[] args) {
		new Gui();
	}


	public static boolean isNumeric(String str) {
		boolean flag = str != null && str.matches("\\d{" + min_len + "," +  max_len +  "}");
		if (!flag) {
			System.out.println(str + " must is " + min_len +  " - " + max_len + "Numeric!");
			return false;
		} else
			return true;
	}

	public static boolean lengthValidate(String lenStr) {
		if (lenStr.length() < min_len || lenStr.length() > max_len) {
			System.out.println("Length must is " + min_len +  " - " + max_len + " Charactors");
			return false;
		} else
			return true;
	}
}// end Gui
